﻿<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>STJson</title>
<link rel="shortcut icon" href="./favicon.png" type="image/png"/>
<link rel="stylesheet" type="text/css" href="./css/stdoc.css"/>
<script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="./js/stdoc.js"></script>
</head>
<body>
<div id="div_model" style="display:none">SINGLE</div>
<div id="div_body">
<div id="div_logo"></div>
<div id="div_left">
<div id="div_left_list">
<ul class='ul_group_root'>
<li>
    <a class='a_node_root anchor_btn' name='a_a' href='#a_a'>STLib.STJson</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='a_b' href='#a_b'>简介</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_c' href='#a_c'>路劲疑问</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_d' href='#a_d'>表达式疑问</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='a_e' href='#a_e'>STJson API</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='a_f' href='#a_f'>数据类型关系</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_g' href='#a_g'>静态函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_h' href='#a_h'>非静态函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_i' href='#a_i'>扩展函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_j' href='#a_j'>字段</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_k' href='#a_k'>索引器</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='a_l' href='#a_l'>STJson [基本应用]</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='a_m' href='#a_m'>object -&gt; string</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_n' href='#a_n'>string -&gt; object</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_o' href='#a_o'>STJson -&gt; object</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_p' href='#a_p'>STJsonConverter</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_q' href='#a_q'>STJsonAttribute</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_r' href='#a_r'>STJsonSetting</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_s' href='#a_s'>json_src</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='a_t' href='#a_t'>STJsonPath</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='a_u' href='#a_u'>选择器</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_v' href='#a_v'>使用方式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_w' href='#a_w'>通配符</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_x' href='#a_x'>深度选择器</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_y' href='#a_y'>列表选择器</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='a_z' href='#a_z'>切片选择器</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_a' href='#b_a'>表达式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_b' href='#b_b'>过滤表达式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_c' href='#b_c'>普通表达式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_d' href='#b_d'>测试表达式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_e' href='#b_e'>内置函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_f' href='#b_f'>内置函数列表</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_g' href='#b_g'>自定义函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_h' href='#b_h'>表达式中的选择器</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_i' href='#b_i'>选择方式</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_j' href='#b_j'>ParsedTokens</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='b_k' href='#b_k'>STJson [高级应用]</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='b_l' href='#b_l'>获取值</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_m' href='#b_m'>设置值</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_n' href='#b_n'>回调函数</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_o' href='#b_o'>PathItem</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_p' href='#b_p'>Clone</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='b_q' href='#b_q'>数据聚合</a>
    <ul>
        <li class='li_node_sub'><a class='anchor_btn' name='b_r' href='#b_r'>sort</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_s' href='#b_s'>group</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_t' href='#b_t'>terms</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_u' href='#b_u'>min, max</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_v' href='#b_v'>avg, sum</a></li>
        <li class='li_node_sub'><a class='anchor_btn' name='b_w' href='#b_w'>STJsonPath.Name</a></li>
    </ul>
</li>
<li>
    <a class='a_node_root anchor_btn' name='b_x' href='#b_x'>THE END</a>
    <ul>
        
    </ul>
</li>
</ul><div class='div_left_space_space'></div>
</div>
<span id="span_time">2023-06-06 -> by:<span>DebugST</span></span>
</div>
<div id="div_right">
<h1 class='h_title anchor_point' name='a_a'>STLib.STJson</h1>
<div><h2 class='h_option anchor_point' name='a_b'>简介</h2></div>
<p><span class='mark'>STLib.STJson</span>是一款基于<span class='mark'>MIT</span>开源协议的<span class='mark'>Json</span>解析库。由<a class='mark' target='_blank' href='https://github.com/DebugST'>DebugST</a>开发，该库纯原生实现不依赖任何库，所以非常轻量便捷，且功能强大。由于没用任何依赖纯原生构建，它可以很容易移植到其他编程语言中。当然由于作者懒的一批，这个概率不是很大。</p>
<p>在设计<span class='mark'>STJson</span>的时候作者没有任何的异议，毕竟<span class='mark'>Json</span>的数据格式不存在有争议的地方，当在设计<span class='mark'>STJsonPath</span>的时候，作者犹豫了，因为作者发现很多存在争议的地方，且无法找到关于<span class='mark'>JsonPath</span>的正式<span class='mark'>RFC</span>文档，仅仅找到一份草案：</p>
<p><a class='mark' target='_blank' href='https://www.ietf.org/archive/id/draft-ietf-jsonpath-base-03.html'>JSONPath: Query expressions for JSON</a></p>
<p>虽然草案中一些疑惑也能得到解决，但是作者仍有一些疑惑，毕竟不是正式的文档。在开发之前作者也好奇其他<span class='mark'>JsonPath</span>的开源库，对于有争议的地方其他库是如何解决的。但很遗憾，问题依然存在。似乎没有库愿意搭理这份草案。既然<span class='mark'>JsonPath</span>还没有正式的<span class='mark'>RFC</span>文档，那么作者也想自立门户。但作者并不会不搭理这份草案，只能说作者会尽可能兼容草案的同时，还会加入一些作者自己的想法。</p>
<div><h2 class='h_option anchor_point' name='a_c'>路劲疑问</h2></div>
<p><span class='mark'>JsonPath</span>是源自于<span class='mark'>XPath</span>的使用方式，总所周知<span class='mark'>XPath</span>是<span class='mark'>XML</span>路径语言（XML Path Language），它是一种用来确定<span class='mark'>XML</span>文档中某部分位置的语言。显而易见<span class='mark'>JsonPath</span>是用于<span class='mark'>Json</span>的。它们两种几乎使用方式相同，但是<span class='mark'>XML</span>和<span class='mark'>Json</span>两种数据格式的差异，必然会有存在不同的地方。</p>
<p>比如在<span class='mark'>XML</span>的元素名中是不存在特殊字符的，可是<span class='mark'>Json</span>的<span class='mark'>Key</span>可以是任意的字符串，假如有一下<span class='mark'>Json</span>数据：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>{ <span style='color:rgba(255,165,0,1)'>"aa"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span> <span style='color:rgba(255,165,0,1)'>"bb"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"cc"</span> <span style='color:rgba(255,255,255,1)'>} }</span></pre>
</div>
<p>在<span class='mark'>JsonPath</span>中我们可以通过路径<span class='mark'>aa.bb</span>获取到值<span class='mark'>cc</span>，这没有任何问题，如果<span class='mark'>Json</span>数据换成下面的呢：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>{ <span style='color:rgba(255,165,0,1)'>"a.a"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span> <span style='color:rgba(255,165,0,1)'>"bb"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"cc"</span> <span style='color:rgba(255,255,255,1)'>} }</span></pre>
</div>
<p>很显然通过<span class='mark'>a.a.bb</span>是无法得到值<span class='mark'>cc</span>的。或许通常情况下<span class='mark'>Json</span>都表示某个对象，而对象是不会存在这么奇怪的属性的。但是对于<span class='mark'>Json</span>的数据格式而言，它的<span class='mark'>Key</span>可以是任意字符。即便在浏览器中通过<span class='mark'>{OBJ_NAME}.a.a.bb</span>也是只会得到报错。但是浏览器访问<span class='mark'>Json</span>元素的方式并不是只用通过<span class='mark'>.</span>的方式去获取，还有索引器<span class='mark'>{OBJ_NAME}['a.a']['bb']</span>是可以得到正确值的。</p>
<p>在<span class='mark'>STJsonPath</span>中允许使用<span class='mark'>'</span>或者<span class='mark'>"</span>，在词法分析器(STJsonPathTokenizer.cs)中会将其标记为<span class='mark'>String</span>，若此字符串在非表达式的作用域内，解析器(STJsonPathParser.cs)会将其重新标记为<span class='mark'>Property</span>作为索引使用，从而避免特殊字符串无法处理的情况，字符串支持<span class='mark'>\</span>进行转义。所以在<span class='mark'>STJsonPath</span>中可以使用<span class='mark'>'a.a'.bb</span>获取到正确值。而在草案中也确实提到过类似的处理方式。</p>
<div><h2 class='h_option anchor_point' name='a_d'>表达式疑问</h2></div>
<p>在草案中有提到支持表达式，且表达式有两种类型，<span class='mark'>()</span>和<span class='mark'>?()</span>。分别代表<span class='mark'>普通表达式</span>和<span class='mark'>过滤表达式</span>。而按照作者的理解，<span class='mark'>普通表达式</span>用于计算出一个值，且将这个值作为<span class='mark'>JsonPath</span>的一部分，比如在很多案例中可以看到的<span class='mark'>$.books[(@.length - 1)]</span>。在执行<span class='mark'>JsonPath</span>的时候会可能对<span class='mark'>Json</span>元素进行层层递归，其中<span class='mark'>@</span>是一个动态变量，表示递归过程中当前正在处理的<span class='mark'>Json</span>元素，根据表达式的表面意思是想获取<span class='mark'>books</span>中的倒数第一个元素。</p>
<p>但是<span class='mark'>@.length</span>如何被执行？<span class='mark'>length</span>是哪里来的？如果<span class='mark'>@</span>是一个数组，作者暂且可以理解为是在对数组求长度？可是如果<span class='mark'>@</span>是一个对象呢？</p>
<p>既然是表达式，那么可以在<span class='mark'>()</span>中写什么样的语法呢？语法规呢？关于这些疑问作者都采用了自己的实现方式，将在稍后的教程中介绍。</p>
<h1 class='h_title anchor_point' name='a_e'>STJson API</h1>
<div><h2 class='h_option anchor_point' name='a_f'>数据类型关系</h2></div>
<div class='div_table'>
<table>
<tr><th>.Net</th><th>STJsonValueType</th><th>.Net</th><th>STJsonValueType</th></tr>
<tr><td>byte</td><td>Long</td><td>sbyte</td><td>Long</td></tr>
<tr class='tr_hight'><td>short</td><td>Long</td><td>ushort</td><td>Long</td></tr>
<tr><td>int</td><td>Long</td><td>uint</td><td>Long</td></tr>
<tr class='tr_hight'><td>long</td><td>Long</td><td>ulong</td><td>Long</td></tr>
<tr><td>float</td><td>Double</td><td>double</td><td>Double</td></tr>
<tr class='tr_hight'><td>decimal</td><td>Double</td><td>bool</td><td>Boolean</td></tr>
<tr><td>char</td><td>String</td><td>string</td><td>String</td></tr>
<tr class='tr_hight'><td>DateTime</td><td>String</td><td>enum</td><td>Long or String</td></tr>
<tr><td>Point</td><td>Array</td><td>PointF</td><td>Array</td></tr>
<tr class='tr_hight'><td>Size</td><td>Array</td><td>SizeF</td><td>Array</td></tr>
<tr><td>Rectangle</td><td>Array</td><td>RectangleF</td><td>Array</td></tr>
<tr class='tr_hight'><td>Color</td><td>Array</td><td>DataTable</td><td>Object</td></tr>
<tr><td>Array</td><td>Array</td><td>ICollection</td><td>Array</td></tr>
<tr class='tr_hight'><td>IDectionary</td><td>Object</td><td>object</td><td>Object</td></tr>
</table>
</div>
<p><span class='mark'>STJson</span>涵盖了常见的基本数据类型，即便不包含在其中，那么最后也会通过执行反射递归对象的属性。</p>
<div><h2 class='h_option anchor_point' name='a_g'>静态函数</h2></div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p>下列中<span class='mark'>(+n)</span>表示有多个重载</p>
</div>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>string</td><td>Serialize(+n)</td><td>将<span class='mark'>object</span>对象序列化为字符串。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Deserialize(string)</td><td>将字符串转换为<span class='mark'>STJson</span>对象序。</td></tr>
<tr><td>T</td><td>Deserialize&lt;T&gt;(+n)</td><td>将对象或字符串转换为目标对象。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>STJson CreateObject()</td><td>创建一个空白对象。</td></tr>
<tr><td>STJson</td><td>STJson CreateArray(params object[])</td><td>创建一个数组对象。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>FromObject(+n)</td><td>将一个对象转换为<span class='mark'>STJson</span>。</td></tr>
<tr><td>string</td><td>Format(+n)</td><td>格式化一个<span class='mark'>Json</span>字符串。</td></tr>
<tr class='tr_hight'><td>void</td><td>AddCustomConverter(+n)</td><td>自定义类型转换器。</td></tr>
<tr><td>void</td><td>RemoveCustomConverter(+n)</td><td>移除自定义转换器。</td></tr>
</table>
</div>
<div><h2 class='h_option anchor_point' name='a_h'>非静态函数</h2></div>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>STJson</td><td>SetItem(+n)</td><td>向对象中添加一个键值对，并返回自己。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>SetKey(string)</td><td>向对象中添加一个<span class='mark'>key</span>，并返回目标对象。</td></tr>
<tr><td>void</td><td>SetValue(+n)</td><td>设置目标对象的值。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Delete(string)</td><td>从对象中移除一个<span class='mark'>key</span>，并返回目标对象。</td></tr>
<tr><td>STJson</td><td>Append(+n)</td><td>向数组对象中添加一个或一些元素，并返回自己。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Insert(+n)</td><td>向数组对象中插入一个元素，并返回自己。</td></tr>
<tr><td>STJson</td><td>RemoveAt(int nIndex)</td><td>从数组对象中删除一个索引。</td></tr>
<tr class='tr_hight'><td>void</td><td>Clear()</td><td>清空所有子元素。</td></tr>
<tr><td>IEnumerator&lt;STJson&gt;</td><td>GetEnumerator()</td><td>获取当前元素中所有子元素。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Clone</td><td>克隆当前元素。</td></tr>
</table>
</div>
<div><h2 class='h_option anchor_point' name='a_i'>扩展函数</h2></div>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>STJson</td><td>Set(+n)</td><td>根据路径(STJsonPath)设置对象。</td></tr>
<tr class='tr_hight'><td>string</td><td>GetValue(+n)</td><td>获取对象的字符串值(仅值类型)</td></tr>
<tr><td>T</td><td>GetValue&lt;T&gt;(+n)</td><td>获取对象值(仅值类型)</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Select(+n)</td><td>在对象中进行数据筛选。</td></tr>
<tr><td>STJson</td><td>SelectFirst(+n)</td><td>在对象中进行数据筛选，并选中第一个结果。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>SelectLast(+n)</td><td>在对象中进行数据筛选，并选中最后一个结果。</td></tr>
<tr><td>STJson</td><td>Group(+n)</td><td>对指定字段进行分组。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Terms(+n)</td><td>对指定字段的值进行个数统计。</td></tr>
<tr><td>STJson</td><td>Sort(+n)</td><td>对指定字段进行排序处理。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Min(+n)</td><td>统计指定字段的最小值。</td></tr>
<tr><td>STJson</td><td>Max(+n)</td><td>统计指定字段的最大值。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Sum(+n)</td><td>统计指定字段的总数。</td></tr>
<tr><td>STJson</td><td>Avg(+n)</td><td>统计指定字段的平均值。</td></tr>
</table>
</div>
<div><h2 class='h_option anchor_point' name='a_j'>字段</h2></div>
<div class='div_table'>
<table>
<tr><th>type</th><th>name</th><th>note</th></tr>
<tr><td>string</td><td>Key</td><td>当前<span class='mark'>STJson</span>父元素的<span class='mark'>Key</span></td></tr>
<tr class='tr_hight'><td>object</td><td>Value</td><td>当前<span class='mark'>STJson</span>的值，若当前<span class='mark'>STJson</span>不是值类型则为<span class='mark'>null</span></td></tr>
<tr><td>bool</td><td>IsNullObject</td><td>当前<span class='mark'>STJson</span>是否为空元素，即无法确定当前<span class='mark'>STJson</span>值的数据类型。</td></tr>
<tr class='tr_hight'><td>int</td><td>Count</td><td>当前<span class='mark'>STJson</span>所包含子元素的个数。</td></tr>
<tr><td>STJsonValueType</td><td>ValueType</td><td>(枚举)当前元素的数据类型。</td></tr>
</table>
</div>
<p><span class='mark'>STJsonValueType</span>为以下值：</p>
<p><span class='mark'>Undefined</span> <span class='mark'>String</span> <span class='mark'>Boolean</span> <span class='mark'>Long</span> <span class='mark'>Double</span> <span class='mark'>Datetime</span> <span class='mark'>Array</span> <span class='mark'>Object</span></p>
<div><h2 class='h_option anchor_point' name='a_k'>索引器</h2></div>
<ul>
<li>STJson STJson[int]</li>
<li>STJson STJson[string]</li>
</ul>
<h1 class='h_title anchor_point' name='a_l'>STJson [基本应用]</h1>
<p><span class='mark'>STJson</span>是一个中间数据类型，它是<span class='mark'>string</span>与<span class='mark'>object</span>之间的桥梁，使用非常便捷，比如：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> st_json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"number"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span>               <span style='color:rgba(0,255,0,1)'>// 函数返回自身 所以可以连续操作</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"boolean"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"this is string"</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"datetime"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>DateTime</span><span style='color:rgba(112,128,144,1)'>.Now</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"array_1"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateArray</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>123</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>))</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"array_2"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(100,149,237,1)'>object</span><span style='color:rgba(255,255,255,1)'>[] {</span> <span style='color:rgba(144,238,144,1)'>123</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"string"</span> <span style='color:rgba(255,255,255,1)'>}))</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"object"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span> key <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"this is a object"</span> <span style='color:rgba(255,255,255,1)'>})</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"null"</span><span style='color:rgba(255,255,255,1)'>,</span> obj<span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>st_json<span style='color:rgba(219,112,147,1)'>.SetKey</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.SetValue</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"this is a test"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>st_json<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span> <span style='color:rgba(0,255,0,1)'>// 4 -> indentation space count
<span class='span_code_line'></span>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"number"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"boolean"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"this is string"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"datetime"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"2023-04-22T21:12:30.6109410+08:00"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"array_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(144,238,144,1)'>123</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"string"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"array_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(144,238,144,1)'>123</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"string"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"object"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"this is a object"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"null"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"this is a test"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p>当执行<span class='mark'>var st_json = new STJson()</span>时，<span class='mark'>st_json</span>为空元素，即<span class='mark'>st_json.IsNullObject = true</span>。因为此时无法确定<span class='mark'>st_json</span>是<span class='mark'>对象</span>还是<span class='mark'>数组</span>或者是<span class='mark'>值</span>。</p>
<p>在<span class='mark'>STJson</span>中不存在类似于<span class='mark'>JArray</span>或<span class='mark'>JObject</span>对象，<span class='mark'>STJson</span>既可以是<span class='mark'>Array</span>也可以是<span class='mark'>Object</span>。<span class='mark'>STJson</span>拥有两个索引器<span class='mark'>[int]</span><span class='mark'>[string]</span>。</p>
</div>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_1 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_1] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_1<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_1<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_2 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>json_2<span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_2] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_2<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_2<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_3 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>json_3<span style='color:rgba(219,112,147,1)'>.Append</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>3</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_3] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_3<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_3<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_4 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>json_4<span style='color:rgba(219,112,147,1)'>.SetValue</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>DateTime</span><span style='color:rgba(112,128,144,1)'>.Now</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_4] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_4<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_4<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_5 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateArray</span><span style='color:rgba(255,255,255,1)'>();</span>          <span style='color:rgba(0,255,0,1)'>// made by static function</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_5] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_5<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_5<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_6 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateObject</span><span style='color:rgba(255,255,255,1)'>();</span>         <span style='color:rgba(0,255,0,1)'>// made by static function</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_6] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_6<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_6<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_7 <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>12</span><span style='color:rgba(255,255,255,1)'>);</span>         <span style='color:rgba(0,255,0,1)'>// made by static function</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[json_3] - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_7<span style='color:rgba(112,128,144,1)'>.IsNullObject</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>" - "</span> <span style='color:rgba(255,255,255,1)'>+</span> json_7<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_1<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> True <span style='color:rgba(255,255,255,1)'>-</span> None
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_2<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Object
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_3<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Array
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_4<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Datetime
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_5<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Array
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_6<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Object
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>json_7<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>-</span> False <span style='color:rgba(255,255,255,1)'>-</span> Long</span></pre>
</div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p>当<span class='mark'>ValuteType = Array</span>时,无法调用<span class='mark'>SetItem(+n)</span>，当<span class='mark'>ValueType = Object</span>时，无法调用<span class='mark'>Append(+n)</span>。但是通过<span class='mark'>SetValue(+n)</span>可强制改变<span class='mark'>ValueType</span>。</p>
</div>
<p>正如上面提到的<span class='mark'>STJson</span>有两个索引器，可以通过索引器访问他们，或者获取值。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_temp <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateArray</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"this is string"</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"array"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>Object</span><span style='color:rgba(255,255,255,1)'>[] {</span> <span style='color:rgba(255,165,0,1)'>"1"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"2"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"3"</span> <span style='color:rgba(255,255,255,1)'>});</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>]);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"array"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>]);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"array"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>long</span>><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,165,0,1)'>"this is string"</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>this is</span> <span style='color:rgba(100,149,237,1)'>string</span>
<span class='span_code_line'></span><span style='color:rgba(255,165,0,1)'>"2"</span>
<span class='span_code_line'></span><span style='color:rgba(144,238,144,1)'>2</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_m'>object -&gt; string</h2></div>
<p>通过上面的例子或许你已经知道怎么将一个对象转换为<span class='mark'>string</span>，通过<span class='mark'>STJson.FromObject(object).ToString(+n)</span>即可，但是有没有可能，其实不用这么麻烦的？比如：<span class='mark'>STJson.Serialize(+n)</span>就可以了？？？</p>
<p>事实上<span class='mark'>STJson.Serialize(+n)</span>的效率会更好，因为它是直接将对象转换为字符串，而不是转换成<span class='mark'>STJson</span>再转换成字符串。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span> key <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"this is test"</span> <span style='color:rgba(255,255,255,1)'>}));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span><span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,165,0,1)'>"this is test"</span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<p>当然你可以有个更友好的输出格式：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span> key <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"this is test"</span> <span style='color:rgba(255,255,255,1)'>},</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"key"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"this is test"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<p>事实上格式化输出是通过调用静态函数<span class='mark'>STJson.Format(+n)</span>完成的。如果你觉得不喜欢作者内置的格式化风格，完全可以自己写一个格式化的函数。或者去修改源码文件<span class='mark'>STJson.Statics.cs:Format(string,int)</span>。</p>
<div><h2 class='h_option anchor_point' name='a_n'>string -&gt; object</h2></div>
<p>事实上代码并不会直接将<span class='mark'>string</span>转换为<span class='mark'>object</span>。因为在那之前必须先对字符串进行解析，确保它是一个正确格式的<span class='mark'>Json</span>。但是做完这个过程的时候已经得到一个<span class='mark'>STJson</span>对象了。最后将<span class='mark'>STJson</span>再转换为<span class='mark'>object</span>。</p>
<p>所以你会在源代码<span class='mark'>STLib.Json.Converter</span>中看到如下文件：</p>
<p><span class='mark'>ObjectToSTJson.cs</span> <span class='mark'>ObjectToString.cs</span> <span class='mark'>STJsonToObject.cs</span> <span class='mark'>StringToSTJson.cs</span></p>
<p>里面并没有<span class='mark'>StringToObject.cs</span>文件，而<span class='mark'>STJson.Deserialize(+n)</span>的源码如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public static</span> <span style='color:rgba(0,255,255,1)'>T</span> <span style='color:rgba(255,0,255,1)'>Deserialize</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>T</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>string</span> strJson<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,255,255,1)'>+</span>n<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>StringToSTJson</span><span style='color:rgba(219,112,147,1)'>.Get</span><span style='color:rgba(255,255,255,1)'>(</span>strJson<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,255,255,1)'>+</span>n<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return</span> <span style='color:rgba(0,255,255,1)'>STJsonToObject</span><span style='color:rgba(219,112,147,1)'>.Get</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>T</span>><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,255,255,1)'>+</span>n<span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_o'>STJson -&gt; object</h2></div>
<p>如何将字符串转换为对象，相信作者不用说明读者也应该知道如何处理，但是这里值得说明的是，<span class='mark'>STJson</span>可以附加到对象中，实现局部更新。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public class</span> <span style='color:rgba(0,255,255,1)'>TestClass</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(100,149,237,1)'>int</span> X<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(100,149,237,1)'>int</span> Y<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>TestClass</span> tc <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>TestClass</span><span style='color:rgba(255,255,255,1)'>() {</span>
<span class='span_code_line'></span>    X <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Y <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>20</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>};</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJson</span> json_test <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"Y"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Deserialize</span><span style='color:rgba(255,255,255,1)'>(</span>json_test<span style='color:rgba(255,255,255,1)'>,</span> tc<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span>tc<span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span> <span style='color:rgba(255,255,255,1)'>{</span><span style='color:rgba(255,165,0,1)'>"X"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"Y"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_p'>STJsonConverter</h2></div>
<p>虽然在<span class='mark'>STJson</span>中内置了很多数据类型的转换，即便没有的数据类型也会被当做<span class='mark'>object</span>做递归处理。但是有时情况并不是很友好。比如：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Rectangle</span> rect <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>Rectangle</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span>rect<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Location"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"IsEmpty"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"X"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"Y"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Size"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"IsEmpty"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"Width"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"Height"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"X"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Y"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Width"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Height"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Left"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Top"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Right"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>110</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Bottom"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>110</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"IsEmpty"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>false</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<p>很显然，这个结果过于复杂，因为<span class='mark'>Rectangle</span>的所有字段都被递归出来了。但是，如果这样呢？</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public class</span> <span style='color:rgba(0,255,255,1)'>RectangleConverter</span> <span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(0,255,255,1)'>STJsonConverter</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public override</span> <span style='color:rgba(100,149,237,1)'>object</span> <span style='color:rgba(255,0,255,1)'>JsonToObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJson</span> json<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return new</span> <span style='color:rgba(0,255,255,1)'>Rectangle</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>            json<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"x"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(),</span>
<span class='span_code_line'></span>            json<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"y"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(),</span>
<span class='span_code_line'></span>            json<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"w"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(),</span>
<span class='span_code_line'></span>            json<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"h"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>());
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public override</span> <span style='color:rgba(0,255,255,1)'>STJson</span> <span style='color:rgba(255,0,255,1)'>ObjectToJson</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>object</span> obj<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(0,255,255,1)'>Rectangle</span> rect <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Rectangle</span><span style='color:rgba(255,255,255,1)'>)</span>obj<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.New</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"x"</span><span style='color:rgba(255,255,255,1)'>,</span> rect<span style='color:rgba(112,128,144,1)'>.X</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"y"</span><span style='color:rgba(255,255,255,1)'>,</span> rect<span style='color:rgba(112,128,144,1)'>.Y</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"w"</span><span style='color:rgba(255,255,255,1)'>,</span> rect<span style='color:rgba(112,128,144,1)'>.Width</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"h"</span><span style='color:rgba(255,255,255,1)'>,</span> rect<span style='color:rgba(112,128,144,1)'>.Height</span><span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public override</span> <span style='color:rgba(100,149,237,1)'>string</span> <span style='color:rgba(255,0,255,1)'>ObjectToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>object</span> obj<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(0,255,0,1)'>//return "{\"x\":" + ... + "}"</span>
<span class='span_code_line'></span>        <span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>this</span><span style='color:rgba(219,112,147,1)'>.ObjectToJson</span><span style='color:rgba(255,255,255,1)'>(</span>t<span style='color:rgba(255,255,255,1)'>,</span> obj<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bProcessed<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>bProcessed<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>return</span> json<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>();
<span class='span_code_line'></span>        }</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return null</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Rectangle</span> rect <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>Rectangle</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.AddCustomConverter</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>typeof</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Rectangle</span><span style='color:rgba(255,255,255,1)'>),</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>RectangleConverter</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>string</span> strResult <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span>rect<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>strResult<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>rect <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Deserialize</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>Rectangle</span>><span style='color:rgba(255,255,255,1)'>(</span>strResult<span style='color:rgba(219,112,147,1)'>.Replace</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"100"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"200"</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>rect<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span><span style='color:rgba(255,165,0,1)'>"x"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"y"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"w"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"h"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>{</span>X<span style='color:rgba(255,255,255,1)'>=</span><span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>Y<span style='color:rgba(255,255,255,1)'>=</span><span style='color:rgba(144,238,144,1)'>10</span><span style='color:rgba(255,255,255,1)'>,</span>Width<span style='color:rgba(255,255,255,1)'>=</span><span style='color:rgba(144,238,144,1)'>200</span><span style='color:rgba(255,255,255,1)'>,</span>Height<span style='color:rgba(255,255,255,1)'>=</span><span style='color:rgba(144,238,144,1)'>200</span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p>其中<span class='mark'>bProcessed</span>默认传入值为<span class='mark'>true</span>，当上一层函数获取到<span class='mark'>false</span>时则使用默认的处理方式。</p>
</div>
<p><span class='mark'>STJsonConverter</span>提供了<span class='mark'>Attribute</span>类，也可用于标记对象属性。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public class</span> <span style='color:rgba(0,255,255,1)'>Test</span><span style='color:rgba(255,255,255,1)'>{
<span class='span_code_line'></span>    [</span><span style='color:rgba(0,255,255,1)'>STJsonConverter</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>typeof</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>RectangleConverter</span><span style='color:rgba(255,255,255,1)'>))]</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(0,255,255,1)'>Rectangle</span> Rect<span style='color:rgba(255,255,255,1)'>{</span><span style='color:rgba(30,144,255,1)'>get</span><span style='color:rgba(255,255,255,1)'>;</span> <span style='color:rgba(30,144,255,1)'>set</span><span style='color:rgba(255,255,255,1)'>;}
<span class='span_code_line'></span>}</span></pre>
</div>
<span style='display:block'><span class='span_code_title'>STJsonConverter.cs</span></span>
<div class='div_code title'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public abstract class</span> <span style='color:rgba(0,255,255,1)'>STJsonConverter</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public virtual</span> <span style='color:rgba(0,255,255,1)'>STJson</span> <span style='color:rgba(255,0,255,1)'>ObjectToJson</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>object</span> obj<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        bProcessed <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return null</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public virtual</span> <span style='color:rgba(100,149,237,1)'>string</span> <span style='color:rgba(255,0,255,1)'>ObjectToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>object</span> obj<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        bProcessed <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return null</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public virtual</span> <span style='color:rgba(100,149,237,1)'>object</span> <span style='color:rgba(255,0,255,1)'>JsonToObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Type</span> t<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJson</span> json<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bool <span style='color:rgba(0,255,255,1)'>bProcessed</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        bProcessed <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return null</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_q'>STJsonAttribute</h2></div>
<p>或许在序列化的时候你并不想输出所有的属性，那么可以通过<span class='mark'>STJsonAttribute</span>去控制。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>[<span style='color:rgba(219,112,147,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonSerilizaMode</span><span style='color:rgba(112,128,144,1)'>.Include</span><span style='color:rgba(255,255,255,1)'>)]</span>    <span style='color:rgba(0,255,0,1)'>// optional</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public class</span> <span style='color:rgba(0,255,255,1)'>Student</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{
<span class='span_code_line'></span>    [</span><span style='color:rgba(0,255,255,1)'>STJsonProperty</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"test_name"</span><span style='color:rgba(255,255,255,1)'>)]</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(100,149,237,1)'>string</span> Name<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(100,149,237,1)'>int</span> Age<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(0,255,255,1)'>Gender</span> Gender<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    [</span>STJsonProperty<span style='color:rgba(255,255,255,1)'>]</span>                        <span style='color:rgba(0,255,0,1)'>// optional</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(0,255,255,1)'>List</span><<span style='color:rgba(100,149,237,1)'>string</span>> Hobby<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public enum</span> <span style='color:rgba(0,255,255,1)'>Gender</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    Male<span style='color:rgba(255,255,255,1)'>,</span> Female
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> stu <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>Student</span><span style='color:rgba(255,255,255,1)'>() {</span>
<span class='span_code_line'></span>    Name <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Age <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Gender <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>Gender</span><span style='color:rgba(112,128,144,1)'>.Male</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Hobby <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>List</span><<span style='color:rgba(100,149,237,1)'>string</span>><span style='color:rgba(255,255,255,1)'>() {</span> <span style='color:rgba(255,165,0,1)'>"Game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Sing"</span> <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>};</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span>str <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span>stu<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>str<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span><span style='color:rgba(255,165,0,1)'>"test_name"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"Hobby"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"Cooking"</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(255,165,0,1)'>"Sports"</span><span style='color:rgba(255,255,255,1)'>]}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_r'>STJsonSetting</h2></div>
<p><span class='mark'>STJsonSetting</span>用于在序列化或者反序列化中添加一些个性化设置。原本设置是全局的。但随着一些设置项的增多，作者认为全局设置粘性太高，所以独立出来<span class='mark'>STJsonSetting</span>用于解耦。同时独立的设置类也可以方便后续版本的功能扩展，当然后续版本的概率不是很大。除非作者不想当咸鱼了。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> stu <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>Student</span><span style='color:rgba(255,255,255,1)'>() {</span>
<span class='span_code_line'></span>    Name <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Age <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Gender <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>Gender</span><span style='color:rgba(112,128,144,1)'>.Male</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    Hobby <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>List</span><<span style='color:rgba(100,149,237,1)'>string</span>><span style='color:rgba(255,255,255,1)'>() {</span> <span style='color:rgba(255,165,0,1)'>"Game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Sing"</span> <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>};</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJsonSetting</span> setting <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJsonSetting</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>setting<span style='color:rgba(112,128,144,1)'>.EnumUseNumber</span> <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>setting<span style='color:rgba(112,128,144,1)'>.IgnoreAttribute</span> <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>setting<span style='color:rgba(112,128,144,1)'>.Mode</span> <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJsonSettingKeyMode</span><span style='color:rgba(112,128,144,1)'>.Exclude</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>setting<span style='color:rgba(112,128,144,1)'>.KeyList</span><span style='color:rgba(219,112,147,1)'>.Add</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"Age"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>str <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Serialize</span><span style='color:rgba(255,255,255,1)'>(</span>stu<span style='color:rgba(255,255,255,1)'>,</span> setting<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Format</span><span style='color:rgba(255,255,255,1)'>(</span>str<span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Deserialize</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>Student</span>><span style='color:rgba(255,255,255,1)'>(</span>str<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"test_name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"Game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Sing"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>}</span></pre>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p><span class='mark'>Attribute</span>的优先级大于<span class='mark'>STJsonSetting</span>。</p>
</div>
<div><h2 class='h_option anchor_point' name='a_s'>json_src</h2></div>
<p>在接下来的教程中我们会使用到一些测试数据，数据如下：</p>
<span style='display:block'><span class='span_code_title'>test.json</span></span>
<div class='div_code title'>
<pre class='pre_code'><span class='span_code_line'></span><span>[<span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>},{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>},{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>},{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>}]</span></pre>
</div>
<p>将其加载到程序中：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_src <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Deserialize</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>System</span><span style='color:rgba(112,128,144,1)'>.IO.File</span><span style='color:rgba(219,112,147,1)'>.ReadAllText</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"./test.json"</span><span style='color:rgba(255,255,255,1)'>));</span></pre>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p>之后的案例中出现<span class='mark'>json_src</span>则为以上对象。</p>
</div>
<h1 class='h_title anchor_point' name='a_t'>STJsonPath</h1>
<p>在源码<span class='mark'>STJsonExtension.cs</span>中对<span class='mark'>STJson</span>的功能进行了扩展，里面集成一些<span class='mark'>STJsonPath</span>的功能。所以在<span class='mark'>STJson</span>的原始代码中并没有对<span class='mark'>STJsonPath</span>的依赖，<span class='mark'>STJson</span>可独立使用。但<span class='mark'>STJsonPath</span>作为<span class='mark'>STJson</span>的辅助类，需依赖<span class='mark'>STJson</span>。</p>
<div><h2 class='h_option anchor_point' name='a_u'>选择器</h2></div>
<div class='div_table'>
<table>
<tr><th>token</th><th>note</th></tr>
<tr><td>$</td><td>根节点选择器，可视作代表根节点对象。</td></tr>
<tr class='tr_hight'><td>@</td><td>当前元素选择器，在遍历过程中指代当前被遍历的元素。</td></tr>
<tr><td>*</td><td>通配符，表示可以代表任何一个节点。</td></tr>
<tr class='tr_hight'><td>.&lt;name&gt;</td><td>子节点选择器，指定子节点的<span class='mark'>key</span>。</td></tr>
<tr><td>..</td><td>深度选择器，表示可以是任意路径。</td></tr>
<tr class='tr_hight'><td>['&lt;name&gt;'(,'&lt;name&gt;')]</td><td>列表选择器，指定子节点的<span class='mark'>key</span>集合。</td></tr>
<tr><td>[&lt;number&gt;(,(number))]</td><td>列表选择器，指定子节点的<span class='mark'>index</span>集合。</td></tr>
<tr class='tr_hight'><td>[start:end:step]</td><td>切片选择器，用于指定索引区间。</td></tr>
<tr><td>[(&lt;expression&gt;)]</td><td>表达式选择器，用于输入一个运算表达式，并将结果作为索引继续向下选择。</td></tr>
<tr class='tr_hight'><td>[?(&lt;expression&gt;)]</td><td>表达式选择器，用于输入一个运算表达式，并将结果转换为布尔值，决定是否继续选择。</td></tr>
</table>
</div>
<div><h2 class='h_option anchor_point' name='a_v'>使用方式</h2></div>
<p>通过以下方式可以构建一个<span class='mark'>STJsonPath</span>：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// var jp = new STJsonPath("$[0]name");
<span class='span_code_line'></span>// var jp = new STJsonPath("$[0].name");</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> jp <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[0]'name'"</span><span style='color:rgba(255,255,255,1)'>);</span> <span style='color:rgba(0,255,0,1)'>// 以上方式均可以使用 $不是必须的</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>jp<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>当然在<span class='mark'>STJson</span>中的扩展函数中已经集成<span class='mark'>STJsonPath</span>，可以通过下面的方式直接使用：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// var jp = new STJsonPath("[0].name");
<span class='span_code_line'></span>// Console.WriteLine(json_src.Select(jp));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[0].name"</span><span style='color:rgba(255,255,255,1)'>));</span> <span style='color:rgba(0,255,0,1)'>// 内部动态构建 STJsonPath
<span class='span_code_line'></span>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p><span class='mark'>STJsonPath</span>以数组的方式返回数据，其返回值是<span class='mark'>STJson</span>而不是<span class='mark'>List&lt;STJson&gt;</span>，<span class='mark'>STJson</span>也可以是数组对象。</p>
<p><span class='mark'>$</span>开头对于<span class='mark'>STJsonPath</span>来说并不是必须的，且内部会移除掉开头的<span class='mark'>$</span>或者<span class='mark'>@</span>，<span class='mark'>$</span><span class='mark'>@</span>仅在表达式中作为对象的变量使用。</p>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p>在表达式中选择器的返回值仅返回选中的第一个结果。而不是数组列表，这点将在后面说明。</p>
</div>
<p>在<span class='mark'>STJsonPath</span>中允许使用<span class='mark'>'</span>或者<span class='mark'>"</span>，比如：<span class='mark'>'a.b'</span> <span class='mark'>"a.b"</span> <span class='mark'>STJsonPath</span>会将其视为一个独立的个体。而不是两个。列如有如下<span class='mark'>Json</span>：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>{
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"a.b"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"this is a test"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<p>很明显通过<span class='mark'>Select("a.b")</span>是无法获取到数据的，需要通过<span class='mark'>Select("'a.b'")</span>。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>string</span> strTemp <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"{\"a.b\": \"this is a test\"}"</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_temp <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.Deserialize</span><span style='color:rgba(255,255,255,1)'>(</span>strTemp<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"a.b"</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_temp<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"'a.b'"</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[]
<span class='span_code_line'></span>[</span><span style='color:rgba(255,165,0,1)'>"this is a test"</span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>在字符串中支持<span class='mark'>\</span>进行转义：<span class='mark'>\r</span><span class='mark'>\n</span><span class='mark'>\t</span><span class='mark'>\f</span><span class='mark'>\b</span><span class='mark'>\a</span><span class='mark'>\v</span><span class='mark'>\0</span><span class='mark'>\x..</span><span class='mark'>\u...</span><span class='mark'>\.</span></p>
<div><h2 class='h_option anchor_point' name='a_w'>通配符</h2></div>
<p>通配符可表示当前层级中的任何一个节点。获取所有人员姓名。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.name"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_x'>深度选择器</h2></div>
<p>深度选择器与通配符类似，但深度选择器可以是任意层级。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..name"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_y'>列表选择器</h2></div>
<p>列表选择器支持<span class='mark'>int</span>和<span class='mark'>string</span>两种方式，虽然在上面<span class='mark'>选择器</span>的表格中列举了两个列表选择器，但是在<span class='mark'>STJsonPath</span>中只有一个列表选择器，它们可以混合使用，比如下面的使用方式都是合法的：</p>
<ul>
<li><span class='mark'>[0, 1, 2, 3]</span></li>
<li><span class='mark'>['key_1', 'key_2', key_3]</span></li>
<li><span class='mark'>[0, 'key_1', 1, 'key_2', 2, key_3]</span></li>
</ul>
<p><span class='mark'>STJsonPath</span>在内部会自动拆分为两个列表选择器，并判断<span class='mark'>STJsonValueType</span>决定使用哪个列表选择器。内部实现代码如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonPathItem</span><span style='color:rgba(112,128,144,1)'>.ItemType.List</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>jsonCurrent<span style='color:rgba(112,128,144,1)'>.ValueType</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Object</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>foreach</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>var</span> v <span style='color:rgba(30,144,255,1)'>in</span> item<span style='color:rgba(112,128,144,1)'>.Keys</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>jsonCurrent<span style='color:rgba(255,255,255,1)'>[</span>v<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>                <span style='color:rgba(30,144,255,1)'>continue</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>            }</span>
<span class='span_code_line'></span>            <span style='color:rgba(0,255,0,1)'>// ...</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>jsonCurrent<span style='color:rgba(112,128,144,1)'>.ValueType</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Array</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>foreach</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>var</span> v <span style='color:rgba(30,144,255,1)'>in</span> item<span style='color:rgba(112,128,144,1)'>.Indices</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            nIndexSliceL <span style='color:rgba(255,255,255,1)'>=</span> v<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceL <span style='color:rgba(255,255,255,1)'>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span> <span style='color:rgba(255,255,255,1)'>+</span> nIndexSliceL<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(30,144,255,1)'>continue</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'>>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span><span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(30,144,255,1)'>continue</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>            <span style='color:rgba(0,255,0,1)'>// ...</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span></pre>
</div>
<p>选择索引为<span class='mark'>0</span>和<span class='mark'>2</span>的元素。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[0,2]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<p>对于<span class='mark'>int</span>索引可以使用负数，比如<span class='mark'>-1</span>则表示获取最后一个元素。当<span class='mark'>STJsonPath</span>检测到负数时候会执行<span class='mark'>STJson.Count - n</span>将结果作为索引。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>//Console.WriteLine(json_src.Select("-1").ToString(4));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[-1]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='a_z'>切片选择器</h2></div>
<p>切片选择器用于在数组中选择一个片段，切片选择器默认值<span class='mark'>[0:-1:1]</span>，在切片选择器内部如下实现：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonPathItem</span><span style='color:rgba(112,128,144,1)'>.ItemType.Slice</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>jsonCurrent<span style='color:rgba(112,128,144,1)'>.ValueType</span> <span style='color:rgba(255,255,255,1)'>!=</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Array</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceL <span style='color:rgba(255,255,255,1)'>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span> <span style='color:rgba(255,255,255,1)'>+</span> nIndexSliceL<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceR <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceR <span style='color:rgba(255,255,255,1)'>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span> <span style='color:rgba(255,255,255,1)'>+</span> nIndexSliceR<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceL <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>else if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'>>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceL <span style='color:rgba(255,255,255,1)'>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span> <span style='color:rgba(255,255,255,1)'>-</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceR <span style='color:rgba(255,255,255,1)'><</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceR <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>else if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceR <span style='color:rgba(255,255,255,1)'>>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span><span style='color:rgba(255,255,255,1)'>)</span> nIndexSliceR <span style='color:rgba(255,255,255,1)'>=</span> jsonCurrent<span style='color:rgba(112,128,144,1)'>.Count</span> <span style='color:rgba(255,255,255,1)'>-</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>nIndexSliceL <span style='color:rgba(255,255,255,1)'>></span> nIndexSliceR<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>for</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>int</span> i <span style='color:rgba(255,255,255,1)'>=</span> nIndexSliceL<span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'>>=</span> nIndexSliceR<span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'>-=</span> item<span style='color:rgba(112,128,144,1)'>.Step</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            <span style='color:rgba(0,255,0,1)'>// ...</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }</span> <span style='color:rgba(30,144,255,1)'>else</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>for</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>int</span> i <span style='color:rgba(255,255,255,1)'>=</span> nIndexSliceL<span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'><=</span> nIndexSliceR<span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'>+=</span> item<span style='color:rgba(112,128,144,1)'>.Step</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            <span style='color:rgba(0,255,0,1)'>// ...</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span></pre>
</div>
<p>所以切片中的三个值等同于<span class='mark'>for</span>循环中的三个条件，所以原理与效果就不再说明。</p>
<div class='div_table'>
<table>
<tr><th>expression</th><th>range</th><th>note</th></tr>
<tr><td>[::]</td><td>0 &lt;= R &lt;= (OBJ).length - 1</td><td>等同于<span class='mark'>*</span></td></tr>
<tr class='tr_hight'><td>[5:]</td><td>5 &lt;= R &lt;= {OBJ}.length - 1</td><td>从第<span class='mark'>6</span>个元素开始，获取所有元素</td></tr>
<tr><td>[-1:0]</td><td>{OBJ}.length - 1 &gt;= R &gt;= 0</td><td>倒序获取数据</td></tr>
<tr class='tr_hight'><td>[0::2]</td><td>0 &lt;= R &lt;= {OBJ}.length - 1</td><td>顺序获取数据，且间隔一个数据</td></tr>
</table>
</div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p>切片选择器中至少出现一个<span class='mark'>:</span>且<span class='mark'>step</span>大于0，否则将获得异常。</p>
</div>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[-1:]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_a'>表达式</h2></div>
<p>在<span class='mark'>[?()]</span>中可支持下列运算符，优先级从上至下依次升高</p>
<hr/>
<ul>
<li><span class='mark'>&amp;&amp;</span> <span class='mark'>||</span></li>
<li><span class='mark'>&lt;</span> <span class='mark'>&lt;=</span> <span class='mark'>&gt;</span> <span class='mark'>&gt;=</span> <span class='mark'>==</span> <span class='mark'>!=</span> <span class='mark'>re</span></li>
<li><span class='mark'>&amp;</span> <span class='mark'>|</span> <span class='mark'>&lt;&lt;</span> <span class='mark'>&gt;&gt;</span> <span class='mark'>^</span> <span class='mark'>~</span></li>
<li><span class='mark'>+</span> <span class='mark'>-</span></li>
<li><span class='mark'>*</span> <span class='mark'>/</span> <span class='mark'>%</span></li>
<li><span class='mark'>in</span> <span class='mark'>nin</span> <span class='mark'>anyof</span></li>
<li><span class='mark'>!</span></li>
</ul>
<div class='div_table'>
<table>
<tr><th>operator</th><th>note</th><th>e.g</th></tr>
<tr><td>re</td><td>正则表达式</td><td>[?(@.name re 'un')]</td></tr>
<tr class='tr_hight'><td>in</td><td>左边的值或数组包含在右边的数组中</td><td>[?(@.age in [16,20])]</td></tr>
<tr><td>nin</td><td>左边的值或数组不包含在右边的数组中</td><td>[?(@.hobby nin ['sing','draw'])]</td></tr>
<tr class='tr_hight'><td>anyof</td><td>左边的值或数组和右边的数组存在交集</td><td>[?(@.hobby anyof ['sing','draw'])]</td></tr>
</table>
</div>
<p>表达式有两种模式：</p>
<ul>
<li><span class='mark'>[?(&lt;expression&gt;)]</span> - 过滤表达式，用于计算出一个布尔值，确定是否继续匹配。</li>
<li><span class='mark'>[(&lt;expression&gt;)]</span> - 普通表达式，用于计算出一个值，并将值作为索引继续匹配。</li>
</ul>
<div><h2 class='h_option anchor_point' name='b_b'>过滤表达式</h2></div>
<p>选中<span class='mark'>name</span>中包含字母<span class='mark'>ku</span>的元素：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>//Console.WriteLine(json_src.Select("*.[?(@.name == 'kun')]").ToString(4));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.[?(@.name re '(?i)ku')]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<p><span class='mark'>(?i)</span>中的<span class='mark'>i</span>表示忽略大小写，其正则表达式以<span class='mark'>.Net</span>中<span class='mark'>Regex</span>为标准。<span class='mark'>(?...)</span>开头则表示设置匹配模式。至于匹配模式自行查阅相关资料。</p>
<p>选中<span class='mark'>hobby</span>不包含<span class='mark'>sing</span>和<span class='mark'>swing</span>的元素：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.[?(@.hobby nin ['sing','draw'])]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_c'>普通表达式</h2></div>
<p>普通表达式会将结果作为<span class='mark'>STJsonPath</span>的部分继续匹配。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.[('na' + 'me')]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>在<span class='mark'>[('na' + 'me')]</span>中<span class='mark'>'na' + 'me'</span>的结果为<span class='mark'>'name'</span>，并且会将这个值作为索引，所以上述效果等同于<span class='mark'>*.name</span>，当然返回值也可以是一个集合。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.[(['na' + 'me', 'age', 0, 1 + 1])]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>26</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>上面表达式的结算结果值为<span class='mark'>['name', 'age', 0, 2]</span>。但是很显然<span class='mark'>0</span>和<span class='mark'>2</span>将不会起到任何作用，因为第二层的数据对象并不是一个数组。</p>
<p>上面的表达式等同于<span class='mark'>*.['name', 'age', 0, 2]</span>。如果将上面的换成第三层会得到下面的结果。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.*.[(['na' + 'me', 'age', 0, 1 + 1])]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>可以看到<span class='mark'>'name'</span>和<span class='mark'>'age'</span>对于<span class='mark'>hobby</span>来说是无效的，因为<span class='mark'>hobby</span>是一个数组。</p>
<div><h2 class='h_option anchor_point' name='b_d'>测试表达式</h2></div>
<p>可能读者并不了解表达式在内部是如何被执行了并且会输出什么样的结果，作者提供了一个静态测试函数<span class='mark'>TestExpression()</span>可用于调试表达式。若有什么不明白的地方测试一下就会看到过程及结果。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.TestExpression</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>           <span style='color:rgba(0,255,0,1)'>// [STJson] 用于替代表达式中出现的 $</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>           <span style='color:rgba(0,255,0,1)'>// [STJson] 用于替代表达式中出现的 @</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"1+2+3"</span>         <span style='color:rgba(0,255,0,1)'>// 表达式文本</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{1 + 2 + 3}"</span><span style='color:rgba(255,255,255,1)'>,</span>        <span style='color:rgba(0,255,0,1)'>// 格式化后的文本 {}表示此部分需要单独执行 如: [1, {1+1}, 3]</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"polish"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"1"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"2"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>" + "</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"3"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>" + "</span> <span style='color:rgba(0,255,0,1)'>// 逆波兰方式排列</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"steps"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>                      <span style='color:rgba(0,255,0,1)'>// 执行步骤</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"excute"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"operator"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"+"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"get_left_token"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>     <span style='color:rgba(0,255,0,1)'>// 计算操作符左边元素的值，表达式左边也可能是一个表达式</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"1"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"1"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>            },</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"get_right_token"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"2"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"2"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>            },</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>             <span style='color:rgba(0,255,0,1)'>// 该步骤执行结果</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"3"</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>        }, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"excute"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"operator"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"+"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"get_left_token"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>     <span style='color:rgba(0,255,0,1)'>// 此时操作符左边的元素为上一步的计算结果</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"3"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"3"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>            },</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"get_right_token"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"3"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"3"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>            },</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"6"</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>        }
<span class='span_code_line'></span>    ],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"check_result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>               <span style='color:rgba(0,255,0,1)'>// 清空波兰表达式数据栈，确定最终输出结果。</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"6"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"6"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    },</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>                     <span style='color:rgba(0,255,0,1)'>// 最终返回值</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"6"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"bool"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span>                <span style='color:rgba(0,255,0,1)'>// 如果用作布尔表达式则转换为 true</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>}</span></pre>
</div>
<p>如果过程不重要，仅仅是想看执行结果。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.TestExpression</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>           <span style='color:rgba(0,255,0,1)'>// [STJson] 用于替代表达式中出现的 $</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>           <span style='color:rgba(0,255,0,1)'>// [STJson] 用于替代表达式中出现的 @</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"1+2+3"</span>         <span style='color:rgba(0,255,0,1)'>// 表达式文本</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.SelectFirst</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>                     <span style='color:rgba(0,255,0,1)'>// 最终返回值</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"6"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"bool"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span>                <span style='color:rgba(0,255,0,1)'>// 如果用作布尔表达式则转换为 true</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_e'>内置函数</h2></div>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>string</td><td>typeof(+n)</td><td>获取数据类型。</td></tr>
<tr class='tr_hight'><td>string</td><td>str(+n)</td><td>转换为字符串。</td></tr>
<tr><td>string</td><td>upper(+n)</td><td>转换为大写。</td></tr>
<tr class='tr_hight'><td>string</td><td>lower(+n)</td><td>转换为小写。</td></tr>
<tr><td>long</td><td>len(+n)</td><td>获取字符串或者数组长度。</td></tr>
<tr class='tr_hight'><td>long</td><td>long(+n)</td><td>转换为整数。</td></tr>
<tr><td>double</td><td>double(+n)</td><td>转换为浮点数。</td></tr>
<tr class='tr_hight'><td>long or double</td><td>abs(+n)</td><td>获取绝对值。</td></tr>
<tr><td>long</td><td>round(+n)</td><td>四舍五入。</td></tr>
<tr class='tr_hight'><td>long</td><td>ceil(+n)</td><td>向上取整。</td></tr>
</table>
</div>
<p><span class='mark'>typeof</span>具有下列返回值：</p>
<p><span class='mark'>string</span> <span class='mark'>long</span> <span class='mark'>double</span> <span class='mark'>boolean</span> <span class='mark'>array</span> <span class='mark'>object</span> <span class='mark'>undefined</span></p>
<p>以上函数可使用如下签名</p>
<ul>
<li>fn(object)</li>
<li>fn(object[])</li>
<li>fn(object,bool)</li>
<li>fn(object[],bool)</li>
</ul>
<p>其中<span class='mark'>bool</span>表示是否期望以数组方式获得结果，如果为<span class='mark'>true</span>则以数组方式输出结果，并且当<span class='mark'>object</span>为数组是，递归其中的数据。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.TestExpression</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"typeof([1+2+3,'abc'], false)"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.SelectFirst</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"bool"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"String"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"array"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// =============================================================================</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.TestExpression</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"typeof([1+2+3,'abc'], true)"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.SelectFirst</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"bool"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Array"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"long"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"string"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>}</span></pre>
</div>
<p>不同的函数第二个参数采用不同的默认值。一下是一些其他函数，读者可以自己通过<span class='mark'>TestExpression()</span>进行效果测试。</p>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>long or double</td><td>max(+1)</td><td>求最大值。</td></tr>
<tr class='tr_hight'><td>long or double</td><td>min(+1)</td><td>求最小值。</td></tr>
<tr><td>long or double</td><td>avg(+1)</td><td>求平均值。</td></tr>
<tr class='tr_hight'><td>long or double</td><td>sum(+1)</td><td>求总和。</td></tr>
<tr><td>string</td><td>trim(+n)</td><td>裁切字符串两端的指定字符。</td></tr>
<tr class='tr_hight'><td>string</td><td>trims(+n)</td><td>裁切字符串开始的指定字符。</td></tr>
<tr><td>string</td><td>trime(+n)</td><td>裁切字符串末尾的指定字符。</td></tr>
<tr class='tr_hight'><td>string_array</td><td>split(+1)</td><td>拆分字符串。</td></tr>
<tr><td>long or string</td><td>time(+n)</td><td>获取或格式化时间戳。</td></tr>
</table>
</div>
<div><h2 class='h_option anchor_point' name='b_f'>内置函数列表</h2></div>
<p>随着版本的迭代更新(如果可能的话)，内置函数可能随时发生变化，通过<span class='mark'>GetBuildInFunctionList()</span>可以查看当前版本支持的内置函数信息。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.GetBuildInFunctionList</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"typeof"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"demos"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"(object) -> typeof('abc')"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"(array,bool) -> typeof(['abc',123],true)"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    },</span>
<span class='span_code_line'></span>    ...
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>读者可以通过<span class='mark'>TestExpression()</span>按照<span class='mark'>demos</span>对函数进行测试。</p>
<p>选中<span class='mark'>hobby</span>长度大于<span class='mark'>2</span>的元素：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..[?(len(@.hobby) > 1 + 1)]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_g'>自定义函数</h2></div>
<p>在为表达式提供内置函数的时候作者也不知道开发者期望内置什么样的函数，随便内置了几个之后干脆不想了。没有什么问题是写代码解决不了的，没错。。。让开发者自己去写。但是开发者要怎么样为<span class='mark'>STJsonPath</span>提供函数呢？</p>
<p><span class='mark'>STJsonPath.CustomFunctions</span>是一个静态字典。用于保存开发者自定义的函数。其函数签名如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(0,255,255,1)'>delegate STJson</span> <span style='color:rgba(255,0,255,1)'>STJsonPathCustomFuncHander</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPathExpFuncArg</span><span style='color:rgba(255,255,255,1)'>[]</span> args<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public struct</span> <span style='color:rgba(0,255,255,1)'>STJsonPathExpFuncArg</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(0,255,255,1)'>STJsonPathExpFuncArgType</span> Type<span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>public</span> <span style='color:rgba(100,149,237,1)'>object</span> Value<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public enum</span> <span style='color:rgba(0,255,255,1)'>STJsonPathExpFuncArgType</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    Undefined<span style='color:rgba(255,255,255,1)'>,</span> Number<span style='color:rgba(255,255,255,1)'>,</span> String<span style='color:rgba(255,255,255,1)'>,</span> Boolean<span style='color:rgba(255,255,255,1)'>,</span> Array<span style='color:rgba(255,255,255,1)'>,</span> Object<span style='color:rgba(255,255,255,1)'>,
<span class='span_code_line'></span>}</span></pre>
</div>
<p>自定义函数返回值统一<span class='mark'>STJson</span>，因为<span class='mark'>STJson</span>可与<span class='mark'>STJsonPath</span>完美衔接，在<span class='mark'>STJsonPath</span>可直接使用<span class='mark'>选择器</span>对自定义函数返回值进行操作。比如下面的案例为<span class='mark'>STJsonPath</span>添加一个<span class='mark'>matches</span>函数用于正则表达式操作。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(112,128,144,1)'>.CustomFunctions</span><span style='color:rgba(219,112,147,1)'>.Add</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"matches"</span><span style='color:rgba(255,255,255,1)'>, (</span>objs<span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(255,255,255,1)'>=></span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> json_ret <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>    json_ret<span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    json_ret<span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"values"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateArray</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>objs<span style='color:rgba(112,128,144,1)'>.Length</span> <span style='color:rgba(255,255,255,1)'>!=</span> <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>return</span> json_ret<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> ms <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>Regex</span><span style='color:rgba(219,112,147,1)'>.Matches</span><span style='color:rgba(255,255,255,1)'>(</span>objs<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(),</span> objs<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span>    json_ret<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.SetValue</span><span style='color:rgba(255,255,255,1)'>(</span>ms<span style='color:rgba(112,128,144,1)'>.Count</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>foreach</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>Match</span> v <span style='color:rgba(30,144,255,1)'>in</span> ms<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        json_ret<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"values"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.Append</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            success <span style='color:rgba(255,255,255,1)'>=</span> v<span style='color:rgba(112,128,144,1)'>.Success</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            index <span style='color:rgba(255,255,255,1)'>=</span> v<span style='color:rgba(112,128,144,1)'>.Index</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            length <span style='color:rgba(255,255,255,1)'>=</span> v<span style='color:rgba(112,128,144,1)'>.Length</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>value</span> <span style='color:rgba(255,255,255,1)'>=</span> v<span style='color:rgba(112,128,144,1)'>.Value</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}));
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return</span> json_ret<span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>});</span></pre>
</div>
<p>然后我们需要筛选出<span class='mark'>hobby</span>中包含两个<span class='mark'>a</span>或<span class='mark'>o</span>的元素：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.hobby.*[?(matches(@,'a|o').count == 2)]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p><span class='mark'>自定义函数</span>优先级高于<span class='mark'>内置函数</span>，也就是说如果在<span class='mark'>自定义函数</span>中和<span class='mark'>内置函数</span>中出现了同名函数，则优先调用<span class='mark'>自定义函数</span>。</p>
</div>
<div><h2 class='h_option anchor_point' name='b_h'>表达式中的选择器</h2></div>
<p>在表达式中的选择器仅返回选中的第一个结果，而不是数组列表。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.TestExpression</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>],</span> json_src<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>],</span> <span style='color:rgba(255,165,0,1)'>"@.name"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"Tom"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{[@]['name']}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"polish"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"[@]['name']"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"steps"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>
<span class='span_code_line'></span>    ],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"check_result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"[@]['name']"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"root_json"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{\"name\":\"Tom\",\"age\":16,\"gender\":0,\"hobby\":[\"cooking\",\"sing\"]}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"current_json"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{\"name\":\"Tom\",\"age\":16,\"gender\":0,\"hobby\":[\"cooking\",\"sing\"]}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"selected_json"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"[\"Tom\"]"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"result"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>                     <span style='color:rgba(0,255,0,1)'>// 返回值将只获取第一个结果</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"String"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    },</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"return"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"bool"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"value_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"String"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"text"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_i'>选择方式</h2></div>
<p>普通模式(默认方式)：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..name"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJsonPathSelectMode</span><span style='color:rgba(112,128,144,1)'>.ItemOnly</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>路径模式：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..name"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJsonPathSelectMode</span><span style='color:rgba(112,128,144,1)'>.ItemWithPath</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"name"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"name"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"name"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(144,238,144,1)'>3</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"name"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>]</span></pre>
</div>
<p>保持结构：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..name"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(0,255,255,1)'>STJsonPathSelectMode</span><span style='color:rgba(112,128,144,1)'>.KeepStructure</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_j'>ParsedTokens</h2></div>
<p><span class='mark'>GetParsedTokens()</span>用于获取当前<span class='mark'>STJsonPath</span>得字符串在内部是如何被解析，且以<span class='mark'>STJson</span>方式输出。如果你也想编写一个解析器，说不定可以给你提供一些思路。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"$..[?(matches(@.name,'u').count == 1)]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.GetParsedTokens</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>)
<span class='span_code_line'></span>    );</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>  <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"entry"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>  <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"[..]{matches({[@]['name']}, {'u'}) == 1}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>  <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Depth"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>".."</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{matches({[@]['name']}, {'u'}) == 1}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>      <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"function"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"matches({[@]['name']}, {'u'})"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"matches"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"args"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"({[@]['name']}, {'u'})"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>              {</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{[@]['name']}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>                  {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>                      {</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Current"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"@"</span>
<span class='span_code_line'></span>                      <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"List"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                          <span style='color:rgba(255,165,0,1)'>"name"</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>                      }
<span class='span_code_line'></span>                    ]
<span class='span_code_line'></span>                  }
<span class='span_code_line'></span>                ]
<span class='span_code_line'></span>              }, {</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"{'u'}"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>                  {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"string"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"u"</span>
<span class='span_code_line'></span>                  <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>                ]
<span class='span_code_line'></span>              }
<span class='span_code_line'></span>            ]
<span class='span_code_line'></span>          },</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"selector"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"parsed"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"['count']"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>              {</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"selector_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"List"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                  <span style='color:rgba(255,165,0,1)'>"count"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>              }
<span class='span_code_line'></span>            ]
<span class='span_code_line'></span>          }
<span class='span_code_line'></span>        }, {</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"long"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"expression_item"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"item_type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"operator"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>          <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"=="</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>      ]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>  ]
<span class='span_code_line'></span>}</span></pre>
</div>
<h1 class='h_title anchor_point' name='b_k'>STJson [高级应用]</h1>
<div><h2 class='h_option anchor_point' name='b_l'>获取值</h2></div>
<p>试想一个场景，作为一个<span class='mark'>WEB</span>后端服务，需要处理前端提交过来的<span class='mark'>Json</span>数据，假定我们期望得到以下<span class='mark'>Json</span>数据：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>{
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"type"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"get_list"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"from"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>100</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"size"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>10</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"other"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{}
<span class='span_code_line'></span>}</span></pre>
</div>
<p>并且我们已经将上面的数据转化为<span class='mark'>STJson</span>且命名为<span class='mark'>json_post</span>。其中<span class='mark'>from</span>和<span class='mark'>size</span>用于翻页功能。那么后台可能会做如下判断：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>if</span><span style='color:rgba(255,255,255,1)'>(</span>json_post<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>) {</span> <span style='color:rgba(0,255,0,1)'>/* do something */</span> <span style='color:rgba(255,255,255,1)'>}</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>if</span><span style='color:rgba(255,255,255,1)'>(</span>json_post<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(255,165,0,1)'>"from"</span><span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>) {</span> <span style='color:rgba(0,255,0,1)'>/* do something */</span> <span style='color:rgba(255,255,255,1)'>}</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>if</span><span style='color:rgba(255,255,255,1)'>(</span>json_post<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(255,165,0,1)'>"from"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(112,128,144,1)'>.ValueType</span> <span style='color:rgba(255,255,255,1)'>!=</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Long</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,0,1)'>/* do something */</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>int</span> nFrom <span style='color:rgba(255,255,255,1)'>=</span> json_post<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(255,165,0,1)'>"from"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// 或者 直接暴力一点</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>int</span> nFrom <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>try</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    nFrom <span style='color:rgba(255,255,255,1)'>=</span> json_post<span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(255,165,0,1)'>"page"</span><span style='color:rgba(255,255,255,1)'>][</span><span style='color:rgba(255,165,0,1)'>"from"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>();
<span class='span_code_line'></span>}</span><span style='color:rgba(30,144,255,1)'>catch</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,0,1)'>/* do something */</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>}</span></pre>
</div>
<p>很显然上面的代码让你抓狂。。。当然为了减少麻烦可以在后端代码中为其创建一个实体对象，然后将<span class='mark'>Json</span>绑定到实体对象中。。。但是为每个<span class='mark'>Post</span>过来的数据类型创建一个实体对象是不是也挺麻烦的。但是如果使用下面的代码。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>int</span> nFrom <span style='color:rgba(255,255,255,1)'>=</span> json_post<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"page.from"</span><span style='color:rgba(255,255,255,1)'>);</span></pre>
</div>
<p>或许你会疑惑，，如果路径不存在或者<span class='mark'>from</span>根本不是数字怎么办？emm....上面的代码依然会报错,因为上面的代码内部调用是：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public static</span> <span style='color:rgba(0,255,255,1)'>T</span> <span style='color:rgba(255,0,255,1)'>GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>T</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>this</span> STJson <span style='color:rgba(0,255,255,1)'>json</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>string</span> strJsonPath<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return</span> json<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>T</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(255,255,255,1)'>(</span>strJsonPath<span style='color:rgba(255,255,255,1)'>));
<span class='span_code_line'></span>}</span>
<span class='span_code_line'></span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public static</span> <span style='color:rgba(0,255,255,1)'>T</span> <span style='color:rgba(255,0,255,1)'>GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>T</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>this</span> STJson <span style='color:rgba(0,255,255,1)'>json</span><span style='color:rgba(255,255,255,1)'>,</span> STJsonPath jsonPath<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> j <span style='color:rgba(255,255,255,1)'>=</span> jsonPath<span style='color:rgba(219,112,147,1)'>.SelectFirst</span><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>j <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>throw new</span> <span style='color:rgba(0,255,255,1)'>STJsonPathException</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"Can not selected a object with path {"</span> <span style='color:rgba(255,255,255,1)'>+</span> jsonPath<span style='color:rgba(112,128,144,1)'>.SourceText</span> <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(255,165,0,1)'>"}"</span><span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> t <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(100,149,237,1)'>typeof</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>T</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>bool</span> bProcessed <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> convert <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJsonBuildInConverter</span><span style='color:rgba(219,112,147,1)'>.Get</span><span style='color:rgba(255,255,255,1)'>(</span>t<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>convert <span style='color:rgba(255,255,255,1)'>!=</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(100,149,237,1)'>var</span> value <span style='color:rgba(255,255,255,1)'>=</span> convert<span style='color:rgba(219,112,147,1)'>.JsonToObject</span><span style='color:rgba(255,255,255,1)'>(</span>t<span style='color:rgba(255,255,255,1)'>,</span> json<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>ref</span> bProcessed<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>if</span> <span style='color:rgba(255,255,255,1)'>(</span>bProcessed<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>            <span style='color:rgba(30,144,255,1)'>return</span> <span style='color:rgba(255,255,255,1)'>(</span>T<span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(30,144,255,1)'>value</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>        }
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return</span> <span style='color:rgba(255,255,255,1)'>(</span>T<span style='color:rgba(255,255,255,1)'>)</span>j<span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>}</span></pre>
</div>
<p>因为当不存在元素时候，不知道需要返回什么值，可能读者认为无法获取值，返回一个默认值就好了，的确可以这么设计，可是调用者如何确定返回的值是真实的值还是默认值。无法确定是否异常了。除非强制指定一个默认值。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>int</span> nFrom <span style='color:rgba(255,255,255,1)'>=</span> json_post<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"page.from"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>);</span></pre>
</div>
<p>如果存在元素且能正常转换则返回元素值，否则返回<span class='mark'>0</span>。</p>
<p>或者想知道是否返回的是真实值：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>int</span> nFrom <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> bFlag <span style='color:rgba(255,255,255,1)'>=</span> json_src<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>int</span>><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"page.from"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>out</span> nFrom<span style='color:rgba(255,255,255,1)'>);</span></pre>
</div>
<div class='div_hightlight' style='background-color:goldenrod;color:black'>
<p>若填写的<span class='mark'>strJsonPath</span>会获取到多个值，则取第一个值。</p>
</div>
<div><h2 class='h_option anchor_point' name='b_m'>设置值</h2></div>
<p>如果我们需要手动构造一个<span class='mark'>Json</span>可以通过<span class='mark'>STJson.SetItem()</span>来添加元素。可是有些场景这样使用似乎有点麻烦，比如以<span class='mark'>ElasticSearch</span>数据库的<span class='mark'>query</span>语法为例。我们需要构造一个下面的数据：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>{
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"query"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"term"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"field_name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"field_value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>}</span></pre>
</div>
<p>那么按照之前的写法我们需要这样去构造一个<span class='mark'>STJson</span>对象：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"query"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>        <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"term"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"field_name"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"field_value"</span><span style='color:rgba(255,255,255,1)'>))
<span class='span_code_line'></span>        );</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// or</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    query <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        term <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            field_name <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"field_value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>});</span></pre>
</div>
<p>但是还可以这样写代码：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.Set</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"query.term.field_name"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"field_value"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"query"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"term"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"field_name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"field_value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>}</span></pre>
</div>
<p>甚至是这样：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.Set</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"array[0:4].[key_1,key_2]"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"array"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_1"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"key_2"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"value"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    ]
<span class='span_code_line'></span>}</span></pre>
</div>
<div class='div_hightlight' style='background-color:pink;color:black'>
<p>在<span class='mark'>Set</span>中仅仅支持<span class='mark'>列表选择器</span>和<span class='mark'>切片选择器</span>。不支持其他选择器。</p>
</div>
<div><h2 class='h_option anchor_point' name='b_n'>回调函数</h2></div>
<p>通过上面的<span class='mark'>Set</span>我们是否能够将<span class='mark'>json_src</span>中每个人的喜好都添加一个<span class='mark'>coding</span>？。。似乎不太可以。毕竟向<span class='mark'>hobby</span>中添加数据需要<span class='mark'>append</span>而不是<span class='mark'>set</span>。</p>
<p>当然也并不是没有办法。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.hobby"</span><span style='color:rgba(255,255,255,1)'>, (</span>p<span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(255,255,255,1)'>=></span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    j<span style='color:rgba(219,112,147,1)'>.Append</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"coding"</span><span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>});</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"coding"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    },</span>
<span class='span_code_line'></span>    ...
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>在<span class='mark'>Select</span>中支持两种回调函数，一个需要返回值，另一个不需要返回值，如同上面的。而下面的带返回值。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.hobby.*"</span><span style='color:rgba(255,255,255,1)'>, (</span>p<span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(255,255,255,1)'>=></span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return new</span> <span style='color:rgba(0,255,255,1)'>STJsonPathCallBackResult</span><span style='color:rgba(255,255,255,1)'>() {</span>
<span class='span_code_line'></span>        Selected <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>            <span style='color:rgba(0,255,0,1)'>// 是否将此条 json 添加到结果中</span>
<span class='span_code_line'></span>        Json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>         <span style='color:rgba(0,255,0,1)'>// 需要被添加到结果的 json</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>,</span> p<span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.ToUpper</span><span style='color:rgba(255,255,255,1)'>())
<span class='span_code_line'></span>    };
<span class='span_code_line'></span>});</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>0</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"COOKING"</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    ...
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_o'>PathItem</h2></div>
<p>在<span class='mark'>STJsonPath</span>中有一个比较特殊的数据结构。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>[
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            ...
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>:</span> ...
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>},</span>
<span class='span_code_line'></span>    ...
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>可以通过<span class='mark'>RestorePathJson()</span>还原其结构。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json <span style='color:rgba(255,255,255,1)'>=</span> json_src<span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.hobby.*"</span><span style='color:rgba(255,255,255,1)'>, (</span>p<span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(255,255,255,1)'>=></span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return new</span> <span style='color:rgba(0,255,255,1)'>STJsonPathCallBackResult</span><span style='color:rgba(255,255,255,1)'>() {</span>
<span class='span_code_line'></span>        Selected <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        Json <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>,</span> p<span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"item"</span><span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.ToUpper</span><span style='color:rgba(255,255,255,1)'>())
<span class='span_code_line'></span>    };
<span class='span_code_line'></span>});</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(219,112,147,1)'>.RestorePathJson</span><span style='color:rgba(255,255,255,1)'>(</span>json<span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"COOKING"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"SING"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"GAME"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"DANCE"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"DRAW"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"SING"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"SING"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"DANCE"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"RAP"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"BASKETBALL"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_p'>Clone</h2></div>
<p>如果想向所有的用户喜好中添加<span class='mark'>coding</span>。但是又不想影响源数据，那么可以克隆一份数据进行操作。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>json_src<span style='color:rgba(219,112,147,1)'>.Clone</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.hobby"</span><span style='color:rgba(255,255,255,1)'>, (</span>p<span style='color:rgba(255,255,255,1)'>,</span> j<span style='color:rgba(255,255,255,1)'>)</span> <span style='color:rgba(255,255,255,1)'>=></span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    j<span style='color:rgba(219,112,147,1)'>.Append</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"coding"</span><span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>});</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    },</span>
<span class='span_code_line'></span>    ...
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<h1 class='h_title anchor_point' name='b_q'>数据聚合</h1>
<p>在<span class='mark'>STJson</span>中内置了部分扩展函数用于聚合操作，进行一些简单的数据处理。结合<span class='mark'>STJsonPath</span>可以快速且简单的完成一些数据操作。</p>
<div><h2 class='h_option anchor_point' name='b_r'>sort</h2></div>
<p><span class='mark'>Sort()</span>用于对数据进行排序，内部采用<span class='mark'>归并排序</span>方式。此函数有如下形式签名：</p>
<div class='div_table'>
<table>
<tr><th>return</th><th>signature</th><th>note</th></tr>
<tr><td>STJson</td><td>Sort()</td><td>不指定<span class='mark'>path</span>进行排序。默认升序。</td></tr>
<tr class='tr_hight'><td>STJson</td><td>Sort(bool)</td><td>不指定<span class='mark'>path</span>进行排序，指定是否为降序。</td></tr>
<tr><td>STJson</td><td>Sort(params object[])</td><td>用于指定<span class='mark'>path</span>或是否降序。</td></tr>
</table>
</div>
<p>不指定<span class='mark'>path</span>进行排序：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> arr_obj <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(100,149,237,1)'>object</span><span style='color:rgba(255,255,255,1)'>[] {</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span> aa <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"aa"</span> <span style='color:rgba(255,255,255,1)'>} };</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_objs <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span>arr_obj<span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_objs<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_objs<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(0,255,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>,{</span><span style='color:rgba(255,165,0,1)'>"aa"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,165,0,1)'>"aa"</span><span style='color:rgba(255,255,255,1)'>},</span><span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>[</span><span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,{</span><span style='color:rgba(255,165,0,1)'>"aa"</span><span style='color:rgba(255,255,255,1)'>:</span><span style='color:rgba(255,165,0,1)'>"aa"</span><span style='color:rgba(255,255,255,1)'>},</span><span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>你是否会感到疑惑为什么会是这样的结果？。。。但是在那之前为什么你不先疑惑一下为什么要用这样的数组去排序呢？</p>
<p>在<span class='mark'>Sort</span>的内部会对每个元素获取出一个数字并记录元素索引，然后对这个数字进行排序，最后根据索引位置重新组合一个数组达到排序目的。对于如何获取元素的数字规则如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>switch</span> <span style='color:rgba(255,255,255,1)'>(</span>item<span style='color:rgba(112,128,144,1)'>.ValueType</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Long</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Double</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>        d_temp <span style='color:rgba(255,255,255,1)'>=</span> item<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>double</span>><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Boolean</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(0,255,0,1)'>// true 被计算成 1</span>
<span class='span_code_line'></span>        d_temp <span style='color:rgba(255,255,255,1)'>=</span> item<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(100,149,237,1)'>bool</span>><span style='color:rgba(255,255,255,1)'>()</span> <span style='color:rgba(255,255,255,1)'>?</span> <span style='color:rgba(144,238,144,1)'>1</span> <span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.Datetime</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>        d_temp <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>Convert</span><span style='color:rgba(219,112,147,1)'>.ToDouble</span><span style='color:rgba(255,255,255,1)'>(</span>item<span style='color:rgba(219,112,147,1)'>.GetValue</span><span style='color:rgba(255,255,255,1)'><</span><span style='color:rgba(0,255,255,1)'>DateTime</span>><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>case</span> <span style='color:rgba(0,255,255,1)'>STJsonValueType</span><span style='color:rgba(112,128,144,1)'>.String</span><span style='color:rgba(255,255,255,1)'>:</span>
<span class='span_code_line'></span>        d_temp <span style='color:rgba(255,255,255,1)'>=</span> item<span style='color:rgba(112,128,144,1)'>.Value</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span> <span style='color:rgba(255,255,255,1)'>?</span> <span style='color:rgba(144,238,144,1)'>0</span> <span style='color:rgba(255,255,255,1)'>:</span> item<span style='color:rgba(112,128,144,1)'>.Value</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>()</span><span style='color:rgba(112,128,144,1)'>.Length</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>default</span><span style='color:rgba(255,255,255,1)'>:</span>    <span style='color:rgba(0,255,0,1)'>// 对于其他类型让其始终排在最后面即可</span>
<span class='span_code_line'></span>        d_temp <span style='color:rgba(255,255,255,1)'>=</span> arr_b_desc<span style='color:rgba(255,255,255,1)'>[</span>j<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>?</span> <span style='color:rgba(100,149,237,1)'>double</span><span style='color:rgba(112,128,144,1)'>.MinValue</span> <span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(100,149,237,1)'>double</span><span style='color:rgba(112,128,144,1)'>.MaxValue</span><span style='color:rgba(255,255,255,1)'>;</span>
<span class='span_code_line'></span>        <span style='color:rgba(30,144,255,1)'>break</span><span style='color:rgba(255,255,255,1)'>;
<span class='span_code_line'></span>}</span></pre>
</div>
<p>所以如果你没有那么奇怪的数组的话，那么一切都会正常起来。如果实在是想只对里面的数字排序怎么办？</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> arr_obj <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(100,149,237,1)'>object</span><span style='color:rgba(255,255,255,1)'>[] {</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>null</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(255,255,255,1)'>{</span> aa <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(255,165,0,1)'>"aa"</span> <span style='color:rgba(255,255,255,1)'>} };</span>
<span class='span_code_line'></span><span style='color:rgba(100,149,237,1)'>var</span> json_objs <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.FromObject</span><span style='color:rgba(255,255,255,1)'>(</span>arr_obj<span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"..[?(typeof(@) in ['long', 'double'])]"</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_objs<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>());</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_objs<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>[</span><span style='color:rgba(144,238,144,1)'>6</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>5</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>,</span><span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<p>指定<span class='mark'>path</span>进行排序：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<p>当然你也可以指定降序排列：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span></pre>
</div>
<p>还可以同时指定多个字段进行排序：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>    {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }, {</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>]</span></pre>
</div>
<p>以上为使用<span class='mark'>gender</span>进行升序，然后对于相同值再对<span class='mark'>age</span>进行降序排列。<span class='mark'>Sort</span>函数参数规则如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(255,0,255,1)'>Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>string</span> strJsonPath<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>bool</span> isDesc<span style='color:rgba(255,255,255,1)'>[,</span> <span style='color:rgba(100,149,237,1)'>string</span> strJsonPath<span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>bool</span> isDesc<span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(255,255,255,1)'>*</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>// STLib.Json.STJsonExtension.cs:Sort()</span>
<span class='span_code_line'></span><span style='color:rgba(30,144,255,1)'>public static</span> <span style='color:rgba(0,255,255,1)'>STJson</span> <span style='color:rgba(255,0,255,1)'>Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>this</span> STJson <span style='color:rgba(0,255,255,1)'>json</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(100,149,237,1)'>params object</span><span style='color:rgba(255,255,255,1)'>[]</span> fields<span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>    <span style='color:rgba(100,149,237,1)'>var</span> jsonSort <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(219,112,147,1)'>.CreateArray</span><span style='color:rgba(255,255,255,1)'>();</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>for</span> <span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(100,149,237,1)'>int</span> i <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'><</span> fields<span style='color:rgba(112,128,144,1)'>.Length</span><span style='color:rgba(255,255,255,1)'>;</span> i <span style='color:rgba(255,255,255,1)'>+=</span> <span style='color:rgba(144,238,144,1)'>2</span><span style='color:rgba(255,255,255,1)'>) {</span>
<span class='span_code_line'></span>        <span style='color:rgba(100,149,237,1)'>var</span> j <span style='color:rgba(255,255,255,1)'>=</span> <span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJson</span><span style='color:rgba(255,255,255,1)'>()</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"path"</span><span style='color:rgba(255,255,255,1)'>,</span> fields<span style='color:rgba(255,255,255,1)'>[</span>i<span style='color:rgba(255,255,255,1)'>]</span> <span style='color:rgba(255,255,255,1)'>==</span> <span style='color:rgba(30,144,255,1)'>null</span> <span style='color:rgba(255,255,255,1)'>?</span> <span style='color:rgba(30,144,255,1)'>null</span> <span style='color:rgba(255,255,255,1)'>:</span> fields<span style='color:rgba(255,255,255,1)'>[</span>i<span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>())</span>
<span class='span_code_line'></span>            <span style='color:rgba(219,112,147,1)'>.SetItem</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"desc"</span><span style='color:rgba(255,255,255,1)'>,</span> i <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(144,238,144,1)'>1</span> <span style='color:rgba(255,255,255,1)'><</span> fields<span style='color:rgba(112,128,144,1)'>.Length</span> <span style='color:rgba(255,255,255,1)'>?</span> <span style='color:rgba(0,255,255,1)'>Convert</span><span style='color:rgba(219,112,147,1)'>.ToBoolean</span><span style='color:rgba(255,255,255,1)'>(</span>fields<span style='color:rgba(255,255,255,1)'>[</span>i <span style='color:rgba(255,255,255,1)'>+</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>])</span> <span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(30,144,255,1)'>false</span><span style='color:rgba(255,255,255,1)'>);</span>
<span class='span_code_line'></span>        jsonSort<span style='color:rgba(219,112,147,1)'>.Append</span><span style='color:rgba(255,255,255,1)'>(</span>j<span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>    }</span>
<span class='span_code_line'></span>    <span style='color:rgba(30,144,255,1)'>return</span> json<span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span>jsonSort<span style='color:rgba(255,255,255,1)'>);
<span class='span_code_line'></span>}</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_s'>group</h2></div>
<p><span class='mark'>Group()</span>用于对数据指定<span class='mark'>path</span>进行分组。使用方式如下：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Group</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>                {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>                }, {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>                }
<span class='span_code_line'></span>            ]
<span class='span_code_line'></span>        }, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>                {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Andy"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>20</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>                }, {</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Kun"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>26</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                        <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>                }
<span class='span_code_line'></span>            ]
<span class='span_code_line'></span>        }
<span class='span_code_line'></span>    ]</span></pre>
</div>
<p>同样的你也可以使用多个<span class='mark'>path</span>进行分组，但是和<span class='mark'>Sort</span>不同的是这些<span class='mark'>path</span>不是对第一个<span class='mark'>path</span>的结果再进行分组。是并列的，而非嵌套的。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Group</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>...<span style='color:rgba(255,255,255,1)'>],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>...<span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>}</span></pre>
</div>
<p>如果想进行嵌套<span class='mark'>Group()</span>应该怎么办？作者并不打算在<span class='mark'>Group()</span>中实现，因为<span class='mark'>STJsonPath</span>可以完成这个工作。</p>
<div><h2 class='h_option anchor_point' name='b_t'>terms</h2></div>
<p><span class='mark'>Terms()</span>与<span class='mark'>ElasticSearch</span>数据库中的聚合类似，统计某个字段的出现次数，与<span class='mark'>Sort()</span>一样，它可以选择是否指定<span class='mark'>path</span>或者多个<span class='mark'>path</span>。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>3</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>2</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"rap"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    ],</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>2</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>2</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    ]
<span class='span_code_line'></span>}</span></pre>
</div>
<p>如你所见，似乎<span class='mark'>hobby</span>并没有对<span class='mark'>count</span>进行排序啊？是的。。。这是作者故意的。。。咋地？不服？。。。有没有可能其实<span class='mark'>Terms()</span>还有一个作用？在<span class='mark'>ES</span>数据库中有个<span class='mark'>cardinality</span>。用作统计某字段去重后的数据个数。</p>
<p>有没有可能其实作者也想实现一个这样的函数？。。但是这个函数写到一半的时候，作者当场Delete。。。没必要啊。。上面的<span class='mark'>Terms()</span>不就已经完成了这个工作了吗？比如：</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>)[</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(112,128,144,1)'>.Count</span><span style='color:rgba(255,255,255,1)'>;</span></pre>
</div>
<p>而且有没有一种可能<span class='mark'>cardinality</span>只统计个数。。。但是。。如果想要获取到去重后的字段值都有哪些要怎么办？。。。而刚才说的这些情况<span class='mark'>Terms()</span>似乎都已经完成了。。仅仅是没有做排序。都已经有<span class='mark'>Sort()</span>函数了，再单独排个序怎么了？</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span>json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>)[</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>]</span><span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>);</span></pre>
</div>
<p>如果想获得人员最受欢迎的前三个喜好要怎么做？</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>
<span class='span_code_line'></span>    json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>)[</span><span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>]</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.Sort</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(30,144,255,1)'>true</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"[0:3]"</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.Select</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"*.value"</span><span style='color:rgba(255,255,255,1)'>)</span>
<span class='span_code_line'></span>    <span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>)
<span class='span_code_line'></span>    );</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>    <span style='color:rgba(0,255,255,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"basketball"</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>]</span></pre>
</div>
<div><h2 class='h_option anchor_point' name='b_u'>min, max</h2></div>
<p><span class='mark'>Min()</span> <span class='mark'>Max()</span>分别为寻找最小值和最大值元素，可以不指定或者指定多个<span class='mark'>path</span>。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Min</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span>                 <span style='color:rgba(0,255,0,1)'>// 其中 4 个元素参与了计算</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>                <span style='color:rgba(0,255,0,1)'>// 最小的 age 值为 16</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"items"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>                  <span style='color:rgba(0,255,0,1)'>// 满足最小值的元素</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tom"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"sing"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>            }, {</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"name"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"Tony"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>16</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"gender"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>0</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,165,0,1)'>"hobby"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[</span>
<span class='span_code_line'></span>                    <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"dance"</span>
<span class='span_code_line'></span>                <span style='color:rgba(255,255,255,1)'>]
<span class='span_code_line'></span>            }
<span class='span_code_line'></span>        ]
<span class='span_code_line'></span>    }
<span class='span_code_line'></span>}</span></pre>
</div>
<p>其实上面的效果有点像<span class='mark'>Group</span>有点像了，再加上一个<span class='mark'>Sort()</span>。。简直就是一模一样。。。连作者自己都懵逼了。。。就应该在<span class='mark'>Min/Max()</span>内部使用<span class='mark'>Group()</span> + <span class='mark'>Sort()</span>实现的。。算了算了。。代码都写完了。但是很显然<span class='mark'>Sort()</span>必然会降低效率。。但是话又说回来。。小数据量不在乎效率。。但是大数据量呢？？？大数据量你给我说你用<span class='mark'>Json</span>数组保存？？？？你食不食油饼？？？</p>
<div><h2 class='h_option anchor_point' name='b_v'>avg, sum</h2></div>
<p><span class='mark'>Avg()</span> <span class='mark'>Sum()</span>分别用于计算平均值和汇总，可以不指定或者指定多个<span class='mark'>key</span>。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Avg</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"age"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>,</span>         <span style='color:rgba(0,255,0,1)'>// 其中 4 个元素参与了计算</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>19</span><span style='color:rgba(112,128,144,1)'>.</span><span style='color:rgba(144,238,144,1)'>5</span>       <span style='color:rgba(0,255,0,1)'>// 平均值 19.5</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>}</span></pre>
</div>
<p><span class='mark'>Sum()</span>返回值与<span class='mark'>Avg()</span>一致，唯一不同的是，<span class='mark'>value</span>一个是平均值，一个是汇总。</p>
<div><h2 class='h_option anchor_point' name='b_w'>STJsonPath.Name</h2></div>
<p>在上面的演示中我们有使用指定一个字段进行数据操作，在结果的<span class='mark'>json</span>中会以这个字段名称作为一个<span class='mark'>key</span>输出。但是上面并没有强调是在指定<span class='mark'>key</span>进行操作，而是<span class='mark'>path</span>。只是路径比较简单让我们看起来像是<span class='mark'>key</span>。</p>
<p>如果这样进行聚合呢？</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"hobby[0]"</span><span style='color:rgba(255,255,255,1)'>)</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"hobby[0]"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    ]
<span class='span_code_line'></span>}</span></pre>
</div>
<p>好像看上去很奇怪的样子。其实<span class='mark'>STJsonPath</span>有一个<span class='mark'>Name</span>属性。</p>
<div class='div_code'>
<pre class='pre_code'><span class='span_code_line'></span><span style='color:rgba(0,255,255,1)'>Console</span><span style='color:rgba(219,112,147,1)'>.WriteLine</span><span style='color:rgba(255,255,255,1)'>(</span>json_src<span style='color:rgba(219,112,147,1)'>.Terms</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(30,144,255,1)'>new</span> <span style='color:rgba(0,255,255,1)'>STJsonPath</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(255,165,0,1)'>"test"</span><span style='color:rgba(255,255,255,1)'>,</span> <span style='color:rgba(255,165,0,1)'>"hobby[0]"</span><span style='color:rgba(255,255,255,1)'>))</span><span style='color:rgba(219,112,147,1)'>.ToString</span><span style='color:rgba(255,255,255,1)'>(</span><span style='color:rgba(144,238,144,1)'>4</span><span style='color:rgba(255,255,255,1)'>));</span>
<span class='span_code_line'></span><span style='color:rgba(0,255,0,1)'>/*******************************************************************************
<span class='span_code_line'></span> *                                [output]                                     *
<span class='span_code_line'></span> *******************************************************************************/</span>
<span class='span_code_line'></span><span style='color:rgba(255,255,255,1)'>{</span>
<span class='span_code_line'></span>    <span style='color:rgba(255,165,0,1)'>"test"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,255,255,1)'>[
<span class='span_code_line'></span>        {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"cooking"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"game"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"draw"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}, {</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"value"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(255,165,0,1)'>"sing"</span><span style='color:rgba(255,255,255,1)'>,</span>
<span class='span_code_line'></span>            <span style='color:rgba(255,165,0,1)'>"count"</span><span style='color:rgba(255,255,255,1)'>:</span> <span style='color:rgba(144,238,144,1)'>1</span>
<span class='span_code_line'></span>        <span style='color:rgba(255,255,255,1)'>}
<span class='span_code_line'></span>    ]
<span class='span_code_line'></span>}</span></pre>
</div>
<h1 class='h_title anchor_point' name='b_x'>THE END</h1>
<p>非常感谢看到最后，作者写的都快要疯掉了，一半的时候就想摆烂放弃了。咬咬牙终于完成了。如果在使用过程中遇到什么问题请一定联系作者，作者一定在第一时间用小本本记录下来。至于改不改。。。日后再说。。</p>
<div class='div_hightlight' style='background-color:black;color:white'>
<ul>
<li>TG: DebugST</li>
<li>QQ: 2212233137</li>
<li>Mail: 2212233137@qq.com</li>
</ul>
</div>

</div>
<div id="div_right_list">
<a name='a_a' href='#a_a'>STLib.STJson</a>
<a name='a_b' href='#a_b'>简介</a>
<a name='a_c' href='#a_c'>路劲疑问</a>
<a name='a_d' href='#a_d'>表达式疑问</a>
<a name='a_e' href='#a_e'>STJson API</a>
<a name='a_f' href='#a_f'>数据类型关系</a>
<a name='a_g' href='#a_g'>静态函数</a>
<a name='a_h' href='#a_h'>非静态函数</a>
<a name='a_i' href='#a_i'>扩展函数</a>
<a name='a_j' href='#a_j'>字段</a>
<a name='a_k' href='#a_k'>索引器</a>
<a name='a_l' href='#a_l'>STJson [基本应用]</a>
<a name='a_m' href='#a_m'>object -&gt; string</a>
<a name='a_n' href='#a_n'>string -&gt; object</a>
<a name='a_o' href='#a_o'>STJson -&gt; object</a>
<a name='a_p' href='#a_p'>STJsonConverter</a>
<a name='a_q' href='#a_q'>STJsonAttribute</a>
<a name='a_r' href='#a_r'>STJsonSetting</a>
<a name='a_s' href='#a_s'>json_src</a>
<a name='a_t' href='#a_t'>STJsonPath</a>
<a name='a_u' href='#a_u'>选择器</a>
<a name='a_v' href='#a_v'>使用方式</a>
<a name='a_w' href='#a_w'>通配符</a>
<a name='a_x' href='#a_x'>深度选择器</a>
<a name='a_y' href='#a_y'>列表选择器</a>
<a name='a_z' href='#a_z'>切片选择器</a>
<a name='b_a' href='#b_a'>表达式</a>
<a name='b_b' href='#b_b'>过滤表达式</a>
<a name='b_c' href='#b_c'>普通表达式</a>
<a name='b_d' href='#b_d'>测试表达式</a>
<a name='b_e' href='#b_e'>内置函数</a>
<a name='b_f' href='#b_f'>内置函数列表</a>
<a name='b_g' href='#b_g'>自定义函数</a>
<a name='b_h' href='#b_h'>表达式中的选择器</a>
<a name='b_i' href='#b_i'>选择方式</a>
<a name='b_j' href='#b_j'>ParsedTokens</a>
<a name='b_k' href='#b_k'>STJson [高级应用]</a>
<a name='b_l' href='#b_l'>获取值</a>
<a name='b_m' href='#b_m'>设置值</a>
<a name='b_n' href='#b_n'>回调函数</a>
<a name='b_o' href='#b_o'>PathItem</a>
<a name='b_p' href='#b_p'>Clone</a>
<a name='b_q' href='#b_q'>数据聚合</a>
<a name='b_r' href='#b_r'>sort</a>
<a name='b_s' href='#b_s'>group</a>
<a name='b_t' href='#b_t'>terms</a>
<a name='b_u' href='#b_u'>min, max</a>
<a name='b_v' href='#b_v'>avg, sum</a>
<a name='b_w' href='#b_w'>STJsonPath.Name</a>
<a name='b_x' href='#b_x'>THE END</a>
</div>
</div>
</body>
</html>
